package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.entity.Goods
import com.zxy.supplier_system.server.service.MarketReturnOrderService
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("markets/returnOrders/{orderId}/items")
class MarketReturnOrdersItemController(
    private val marketReturnOrderService: MarketReturnOrderService
) {

    data class AddMarketReturnOrdersItemRequest(
        val goodsId:Int,
        val amount:Int
    )

    @HasAnyAuthority(Authority.卖场_退货单_下单,Authority.卖场_退货单_修改)
    @PostMapping
    fun addItem( @PathVariable orderId: Int,@RequestBody addMarketReturnOrdersItemRequest: AddMarketReturnOrdersItemRequest){
        this.marketReturnOrderService.addItem(orderId,addMarketReturnOrdersItemRequest)
    }

    @HasAnyAuthority(Authority.卖场_退货单_下单,Authority.卖场_退货单_修改)
    @DeleteMapping("{goodsId}")
    fun deleteItem(@PathVariable goodsId: Int, @PathVariable orderId: Int){
        this.marketReturnOrderService.deleteItem(orderId,goodsId)
    }

    @Transactional
    class UpdateItemAmountRequest(
        val amount: Int
    )

    @HasAnyAuthority(Authority.卖场_退货单_下单,Authority.卖场_退货单_修改)
    @PatchMapping("{goodsId}/amount")
    fun updateItemAmount(@PathVariable goodsId: Int, @PathVariable orderId: Int,@RequestBody updateItemAmountRequest: UpdateItemAmountRequest){
        return this.marketReturnOrderService.updateItemAmount(goodsId,orderId,updateItemAmountRequest)
    }

    @HasAuthority(Authority.卖场_退货单_查询)
    @GetMapping
    fun queryOrderItemsInOrder(@PathVariable orderId: Int,pageable: Pageable):Page<OrderItemInOrderSummary>{
        return this.marketReturnOrderService.queryOrderItemsInOrder(orderId,pageable)
    }

    @HasAnyAuthority(Authority.卖场_退货单_下单,Authority.卖场_退货单_修改,)
    @GetMapping("{goodsId}/exists")
    fun exists(@PathVariable goodsId: Int, @PathVariable orderId: Int): Boolean {
        return this.marketReturnOrderService.exists(orderId,goodsId)
    }

    @HasAnyAuthority(Authority.卖场_退货单_下单,Authority.卖场_退货单_修改,)
    @GetMapping("barcodes/{barcode}/exists")
    fun exists(@PathVariable barcode: String, @PathVariable orderId: Int): Boolean {
        return this.marketReturnOrderService.exists(orderId,barcode)
    }

    data class OrderItemInOrderSummary(
        val goodsId:Int,
        val name:String,
        val barcode:String,
        val amountInOrder: Int,
        val inStore:Int,
        val boxSpecification:String?
    ){
        companion object{
            fun fromGoodsAndAmount(goods: Goods,amount:Int): OrderItemInOrderSummary {
                return OrderItemInOrderSummary(
                    goods.id,
                    goods.name,
                    goods.barcode,
                    amount,
                    goods.amount,
                    goods.boxSpecification,
                )
            }
        }
    }

    @HasAnyAuthority(Authority.卖场_退货单_下单,Authority.卖场_退货单_修改)
    @PostMapping("barcodes/{barcode}")
    fun addItem(@PathVariable barcode: String, @PathVariable orderId: Int):OrderItemInOrderSummary{
        return this.marketReturnOrderService.addItem(orderId,barcode)
    }

    @HasAnyAuthority(Authority.卖场_退货单_下单,Authority.卖场_退货单_修改)
    @PostMapping("goods/{goodsId}")
    fun addItem(@PathVariable goodsId: Int, @PathVariable orderId: Int):OrderItemInOrderSummary{
        return this.marketReturnOrderService.addItem(orderId,goodsId)
    }

}